package hichecker.ui.components.icon_factory;

import java.net.URL;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

import org.apache.log4j.Logger;

public class IconFactory {
	private static Logger logger = Logger.getLogger(IconFactory.class);

	public enum IconType {
		NEW, OPEN, SAVE, SAVE_AS, ADD_PAGE, IMPORT_PAGE, EXPORT_PAGE, SAVE_AS_CSV, SWITCH,
		PACKAGE, FOLDER_PVL, FOLDER_PTL, PAGE, IMPORTED_FILE,
		ADD_PVL, ADD_PTL, ADD_SERIES, REMOVE, CLOSE, DUMMY,
		WARNING,
		OK, GROUP, GROUP_CLEAR,
		ADD_AND, ADD_OR, ADD_STATEMENT
	};

	public static Icon getIcon(IconType icon) {
		return createImageIcon(icon);
	}

	public static JButton getIconButton(String title, String toolTip,
			IconType icon, AbstractAction action) {
		JButton button = icon == null ? new JButton(title) : new JButton(title,
				createImageIcon(icon));
		button.setToolTipText(toolTip);
		button.addActionListener(action);

		return button;
	}

	/**
	 * Erzeugt einen kleinen IconButton mit geänderten inneren Rändern
	 * @param title
	 * @param toolTip
	 * @param icon
	 * @param action
	 * @return
	 */
	public static JButton getSmallIconButton(String title, String toolTip,
			IconType icon, AbstractAction action) {
		JButton button = getIconButton(title, toolTip, icon, action);
		button.putClientProperty("JComponent.sizeVariant", "small");
		button.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
		return button;
	}

	/**
	 * Erzeugt ein ImageIcon objekt aus einem Pfad
	 */
	private static ImageIcon createImageIcon(IconType icon) {
		String path = (icon + ".png").toLowerCase();
		URL imgURL = IconFactory.class.getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			logger.error("Couldn't find file: " + path);
			return null;
		}
	}

}
